<template>
    <div class="app-container">
        <el-form :inline="true" :model="queryParams" label-width="68px" ref="queryForm" size="small"
                 style="text-align: left" v-show="showSearch">
            <el-form-item label="手机号码" prop="userPhone">
                <el-input
                        @keyup.enter.native="handleQuery"
                        clearable
                        placeholder="请输入手机号码"
                        style="width: 240px"
                        v-model="queryParams.userPhone"
                />
            </el-form-item>
            <el-form-item>
                <el-button @click="handleQuery" icon="Search" size="mini" type="warning">搜索</el-button>
                <el-button @click="resetQuery" icon="Refresh" size="mini">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8" style="margin: 10px 0px">
<!--            <el-col :span="1.5">-->
<!--                <el-button-->
<!--                        type="primary"-->
<!--                        plain-->
<!--                        size="mini"-->
<!--                        @click="addButton()"-->
<!--                ><el-icon><EditPen /></el-icon>体检预约</el-button>-->
<!--            </el-col>-->
<!--            <el-col :span="1.5">-->
<!--                <el-button-->
<!--                        :disabled="multiple"-->
<!--                        @click="handleDelete"-->
<!--                        icon="Delete"-->
<!--                        plain-->
<!--                        size="mini"-->
<!--                        type="danger"-->
<!--                >删除-->
<!--                </el-button>-->
<!--            </el-col>-->
        </el-row>

        <el-card class="box-card">
            <template #header>
                <div class="card-header">
                    <span>体检预约记录</span>
                    <el-button class="button" text>Physical Examination Appointment Records</el-button>
                </div>
            </template>
            <el-table :data="list" row-key="userId" v-loading="loading">
                <el-table-column align="center" type="index" width="50"/>
                <el-table-column :show-overflow-tooltip="true" align="center" label="就诊人" prop="userName"/>
                <el-table-column align="center" label="预约时间" prop="time">
                    <template #default="scope">
                        <span>{{ $filters.formatDate(scope.row.time) }}</span>
                    </template>
                </el-table-column>
              <el-table-column align="center" label="医院名称" prop="hospital"/>
              <el-table-column align="center" label="套餐名称" prop="taocanName"/>
                <el-table-column align="center" label="联系电话" prop="userPhone"/>
                <el-table-column align="center" label="证件号" prop="idCard"/>
                <el-table-column align="center" label="预约结果">
                    <template #default="scope">
                        <el-tag type="warning" v-if="scope.row.status==='0'">预约处理中</el-tag>
                        <el-tag type="success" v-if="scope.row.status==='1'">预约成功</el-tag>
                        <el-tag type="danger" v-if="scope.row.status==='2'">预约失败</el-tag>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="预约时间" prop="createTime">
                    <template #default="scope">
                        <span>{{ $filters.formatDate(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        align="center"
                        class-name="small-padding fixed-width"
                        label="操作"
                        width="160"
                >
                    <template #default="scope">
                        <!--                    <el-button-->
                        <!--                            @click="handleUpdate(scope.row)"-->
                        <!--                            icon="Edit"-->
                        <!--                            size="mini"-->
                        <!--                            type="text"-->
                        <!--                    >修改-->
                        <!--                    </el-button>-->
                        <el-button
                                @click="keep(scope.row)"
                                v-if="scope.row.status==='1' || scope.row.status==='2'"
                                icon="Pointer"
                                size="mini"
                                style="color: #3cb71c"
                                type="text"
                        >查看预约单
                        </el-button>
                        <el-button
                                @click="handleDelete(scope.row)"
                                icon="Delete"
                                size="mini"
                                style="color: #FFB101"
                                type="text"
                        >取消预约
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <template v-if="list.length!=0">
                <el-pagination
                        :page-sizes="[5, 10, 15, 20]"
                        :total="total"
                        @current-change="handleCurrentChange"
                        @size-change="handleSizeChange"
                        layout="total, sizes, prev, pager, next, jumper"
                        v-model:current-page="queryParams.pageNum"
                        v-model:page-size="queryParams.pageSize"
                />
            </template>
        </el-card>

        <el-drawer
                ref="drawerRef"
                v-model="dialog"
                title="体检预约"
                direction="rtl"
                class="demo-drawer"
        >
            <div style="padding: 20px 20px">
                <el-card class="box-card" style="text-align: left;line-height: 35px;margin: 20px 0px">
                    <h3>网上预约（3天出报告）</h3>
                    <p><el-icon><Location /></el-icon>xxx医院体检中心</p>
                    <p style="color: #42b983"><el-icon><CircleCheck /></el-icon>购买后立即生效，请按时到院进行体检</p>
                </el-card>

                <el-card class="box-card" style="text-align: left;line-height: 35px">
                    <el-form :model="form" ref="ruleFormRef" :rules="rules">
                        <el-form-item label="体检日期" prop="time">
                            <el-date-picker
                                    v-model="form.time"
                                    type="date"
                                    placeholder="请选择体检日期"
                            />
                        </el-form-item>
                        <el-form-item label="就诊人" prop="userName">
                            <el-input v-model="form.userName" autocomplete="off" />
                        </el-form-item>
                        <el-form-item label="身份证号" prop="idCard">
                            <el-input v-model="form.idCard" autocomplete="off" />
                        </el-form-item>
                        <el-form-item label="联系电话" prop="userPhone">
                            <el-input v-model="form.userPhone" autocomplete="off" />
                        </el-form-item>
                    </el-form>
                    <div style="display: flex;justify-content: space-between;padding: 0px 30px">
                        <span style="color: red;font-size: 22px;font-weight: bold">￥248.00</span>
                        <el-button type="success" @click="submitForm">立即预约</el-button>
                    </div>
                </el-card>
            </div>
        </el-drawer>

        <el-drawer
                ref="drawerRef"
                v-model="dialog2"
                title="体检预约报告"
                direction="rtl"
                class="demo-drawer"
        >
            <div style="padding: 20px 20px">
                <el-card class="box-card" style="text-align: left;line-height: 35px;margin: 20px 0px">
                    <h3>体检预约报告</h3>
                    <p><el-icon><Location /></el-icon>xxx医院体检中心</p>
                    <p style="color: #42b983" v-if="detail.status==='1'"><el-icon><CircleCheck /></el-icon>你已成功预约体检，请准备好相关证件材料准时参加体检！</p>
                    <p style="color: #c20f2a" v-if="detail.status==='2'"><el-icon><CircleCheck /></el-icon>您的体检预约申请失败，您可选择其他时间再次预约！</p>
                </el-card>

                <el-card class="box-card" style="text-align: left;line-height: 35px">
                    <el-descriptions title="预约报告" column="1">
                        <el-descriptions-item label="体检编号"  v-if="detail.status==='1'"><h2>{{detail.appointmentNumber}}</h2></el-descriptions-item>
                        <el-descriptions-item label="体检人">{{detail.userName}}</el-descriptions-item>
                        <el-descriptions-item label="体检人">{{detail.userPhone}}</el-descriptions-item>
                        <el-descriptions-item label="证件号">{{detail.idCard}}</el-descriptions-item>
                        <el-descriptions-item label="预约时间">
                            {{ $filters.formatDate(detail.time) }}
                        </el-descriptions-item>
                    </el-descriptions>
<!--                    <div style="display: flex;justify-content: space-between;padding: 0px 30px">-->
<!--                        <el-button type="success" @click="submitForm">立即预约</el-button>-->
<!--                    </div>-->
                </el-card>
            </div>
        </el-drawer>

    </div>
</template>

<script>

    export default {
        name: "User",
        data() {
            return {
                detail:{},
                dialog2:false,
                dialog:false,
                dialogFormVisible:false,
                // 遮罩层
                loading: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 用户表格数据
                list: [],
                // 弹出层标题
                title: "",
                // 表单参数
                form: {},
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    userPhone: '',
                },
                roleOptions:[
                    {
                        label:"普通用户",
                        value:"U",
                    },
                    {
                        label:"管理员",
                        value: "A",
                    }
                ],
                // 表单校验
                rules: {
                    time: [
                        {required: true, message: "请选择体检时间", trigger: "blur"},
                    ],
                    userName: [
                        {required: true, message: "用户名称不能为空", trigger: "blur"},
                        {min: 2, max: 12, message: '用户名称长度必须介于 2 和 12 之间', trigger: 'blur'}
                    ],
                    userPhone: [
                        {required: true, message: "用户手机号不能为空", trigger: "blur"},
                        {
                            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
                            message: "请输入正确的手机号码",
                            trigger: "blur"
                        }
                    ],
                    idCard: [
                        {required: true, message: "用户手机号不能为空", trigger: "blur"},
                        {
                            pattern: /^[1-9]\d{5}(19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[Xx\d]$/,
                            message: "请输入正确的身份证号",
                            trigger: "blur"
                        }
                    ],
                }
            };
        },
        created() {
            this.getUserList();
        },
        methods: {

            keep(row){
                this.detail=row;
                this.dialog2=true;
            },

            addButton(){
                this.reset();
                this.dialog=true;
            },

            async getUserList() {
                this.loading = true;
                const {data: res} = await this.$http.get('appointment/getAppointmentList', {params:this.queryParams})
                console.log(res)
                this.list = res.data.children
                this.total = res.data.total
                this.loading = false
            },
            // 取消按钮
            cancel() {
                this.dialogFormVisible = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: 0,
                    time: "",
                    userName: "",
                    userPhone: "",
                    status: "0",
                    idCard: "",
                };
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.getUserList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                // 查询参数
                this.queryParams = {
                    pageNum: 1,
                    pageSize: 10,
                    userPhone: "",
                },
                    this.getUserList();
            },
            /** 修改按钮操作 */
            async handleUpdate(row) {
                console.log(row)
                this.form = JSON.parse(JSON.stringify(row));
                this.title = "更新用户信息";
                this.dialogFormVisible = true;
            },
            /** 提交按钮 */
            async submitForm() {
                await this.$refs["ruleFormRef"].validate(valid => {
                    if (valid) {
                        if (this.form.id != 0) {
                            this.modifyFrom();
                        } else {
                            this.addFromFun();
                        }
                    }
                });
                this.dialog = false;
            },
            /**修改用户*/
            async modifyFrom() {
                const {data: res} = await this.$http.post('user/updateUser', this.form);
                if (res.code == 200) {
                    this.getUserList();
                    this.$message.success('修改成功!')
                } else {
                    this.$message.error('服务器异常')
                }
            },
            /**
             * 添加用户
             * */
            async addFromFun() {
                const {data: res} = await this.$http.post('appointment/insertAppointment', this.form);
                console.log(res)
                if (res.code === 200) {
                    this.$message.success('预约申请成功，将在三个工作日给出结果，注意查看！')
                    this.getUserList();
                } else if (res.code === 50003) {
                    this.$message.error('您有预约未完成，请勿多次预约！')
                } else {
                    this.$message.error('服务器异常')
                }
            },
            /** 删除按钮操作 */
            async handleDelete(row) {
                this.$confirm('是否确定取消预约?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    const {data: res} = await this.$http.get('appointment/delAppointment', {params:{
                        id:row.id
                        }});
                    console.log(res)
                    if (res.code == 200) {
                        this.getUserList();
                        this.$message.success('取消成功！')
                    } else {
                        this.$message.error('服务器异常！')
                    }
                }).catch(() => {
                    this.$message.info('已取消删除！')
                })
            },
            handleSizeChange(newSize) {
                this.queryParams.pageSize = newSize;
                this.getUserList();
            }
            ,
            handleCurrentChange(current) {
                this.queryParams.pageNum = current;
                this.getUserList();
            },
        }
    };
</script>
